/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package perl;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 * Cette classe qui hérite la classe JFrame construit la fenêtre d'acceuil de l'application.
 * @author NGUYEN Dac Cong Tai
 */
public class PerlApp extends JFrame implements ActionListener{
    private JPanel mainPanel;
    private JLabel description;
    private JLabel imageLabel;
    private JButton open; //nouveau bouton open
    private JButton exit;
    private JSeparator separator;
    private String codeLangue;
    private String user;

    /**
     * Constructeur en 2 paramètres.
     * @param langue le langage d'affichage de l'application
     * @param utilisateur l'utilisateur de l'application
     */
    public PerlApp(String langue, String utilisateur){
        try {
            user = utilisateur;
            codeLangue = langue;
            Element rootEl = Utility.getElement(Utility.getPath(this, "/Fichiers systeme/ParametresPerl/InterfacePerl.xml"));
            Element acceuil = (Element) rootEl.getElementsByTagName("FAccueil").item(0);
            String caption = ((Element)acceuil.getElementsByTagName("Caption").item(0)).getElementsByTagName(langue).item(0).getTextContent();
            setTitle(caption+" - "+utilisateur);
            setIconImage(Toolkit.getDefaultToolkit().getImage(Utility.getPath(this,"/Fichiers systeme/Images/visualisation.png")));

            initComponents(acceuil);
            //setVisible(true);
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Initialise la fenêtre.
     * @param acceuil l'élément contenant des informations d'application
     * @throws UnsupportedEncodingException
     */
    private void initComponents(Element acceuil) throws UnsupportedEncodingException{
        mainPanel=new JPanel();
        open = new JButton();
        exit = new JButton();
        description = new JLabel();
        imageLabel = new JLabel();
        separator = new JSeparator(javax.swing.SwingConstants.VERTICAL);
        
        open.setText("<html>"+((Element)acceuil.getElementsByTagName("BtOuvrir").item(0)).getElementsByTagName(codeLangue).item(0).getTextContent());
        open.setActionCommand("open");
        open.addActionListener(this);

        exit.setText("<html>"+((Element)acceuil.getElementsByTagName("BtQuitter").item(0)).getElementsByTagName(codeLangue).item(0).getTextContent());
        exit.setActionCommand("exit");
        exit.addActionListener(this);

        description.setText("<html>"+((Element)acceuil.getElementsByTagName("PerlText").item(0)).getElementsByTagName(codeLangue).item(0).getTextContent());

        mainPanel.setBackground(new Color(250, 230, 230));
        mainPanel.setPreferredSize(new Dimension(490, 320));
        mainPanel.setOpaque(true);

        ImageIcon im = new ImageIcon (Toolkit.getDefaultToolkit().getImage(Utility.getPath(this,"/Fichiers systeme/Images/PERL.png")));
        imageLabel.setIcon(im);
        separator.setPreferredSize(new Dimension(20,280));
        separator.setForeground(new Color(172, 168, 153));
        separator.setBackground(Color.white);

        GroupLayout mainPanelLayout = new GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(imageLabel, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(description, 200,200,200)))
                .addGap(20, 20, 20)
                .addComponent(separator, 10, 10, 10)
                .addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(25,25, 25)
                        .addComponent(open, 160,160,160))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(exit, 160, 160, 160)))
                .addGap(50, 50, 50))
        );

        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(imageLabel, GroupLayout.PREFERRED_SIZE,190, GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, 0)
                        .addComponent(description, 72,72,81))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(separator, 250, 250, 250))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(80, 80, 80)
                        .addComponent(open, 81, 81,81)
                        .addGap(40, 40, 40)
                        .addComponent(exit, 81,81,81)))
                .addContainerGap(97, Short.MAX_VALUE))
        );

        
        getContentPane().add(mainPanel);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        pack();
    }

    /**
     * Définit les actions des boutons.
     * @param e l'action
     */
    public void actionPerformed(ActionEvent e) {
        String c = e.getActionCommand();
        if(c.equals("open")){
            try {
                JFileChooser jfc = Utility.ouvrirOpenBox(Utility.getPath(this,"/Fichiers utilisateur/Profils Eprofilea"), null);
                if(jfc!=null){
                    String URL = jfc.getSelectedFile().getAbsolutePath();
                    dispose();
                    App.getApplication().show(new PerlView(Utility.getElement(Utility.getPath(this, "/Fichiers systeme/ParametresPerl/InterfacePerl.xml")),URL, codeLangue, user));
                }
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(PerlApp.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(PerlApp.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SAXException ex) {
                Logger.getLogger(PerlApp.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(PerlApp.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (c.equals("exit")){
            Utility.exit();
        }
    }
}
